{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "\"\"\"\n",
    "闭包\n",
    "嵌套函数，函数内部声明一个函数\n",
    "\"\"\"\n",
    "def func_out():\n",
    "    print(\"我是外部函数\")\n",
    "    def func_in():\n",
    "        print(\"我是内部函数\")\n",
    "    return func_in()\n",
    "\"\"\"\n",
    "一个函数可以返回一个函数，这个函数就叫闭包\n",
    "\"\"\"\n",
    "func_out()"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "def wrapper(exp):\n",
    "    def exp_of(base):\n",
    "        return base ** exp\n",
    "    # 返回内部函数,不能加()\n",
    "    return exp_of\n",
    "\n",
    "square = wrapper(2)\n",
    "print(square(3))\n",
    "print(square(5))\n",
    "print(square(9))"
   ],
   "id": "41a0d58440f9acea",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
